<?php

namespace App\Http\Controllers\Admin;

use App\Models\Order;
use App\Models\OrderLease;
use App\Models\OrderRate;
use App\Models\OrderRefund;
use App\Models\ShopTrans;
use App\Services\CommonService;
use App\Services\ExportService;
use App\Services\GoodsService;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FinanceCheckController extends BaseController
{
    /**
     * 账单结算列表
     */
    public function orderLeaseSettleList(Request $request){
        $settle_status = $request->input('settle_status',OrderLease::ORDER_LEASE_SETTLE_STATUS_NO);
        $order_no = $request->input('order_no','');
        $user_name = $request->input('user_name','');
        $shop_name = $request->input('shop_name','');
        $lease_term = $request->input('lease_term','');
        $pay_time = $request->input('pay_time','');
        $start_time = $request->input('start_time','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        $where = [];
        if(!empty($order_no)){
            $where[] = ['o.trade_no','like','%'.$order_no.'%'];
        }
        if(!empty($user_name)){
            $where1[] = ['m.cert_name','like','%'.$user_name.'%'];
        }
        if(!empty($shop_name)){
            $where[] = ['s.shopname','like','%'.$shop_name.'%'];
        }
        if(!empty($lease_term)){
            $where[] = ['ol.which_period',$lease_term];
        }
        if(!empty($pay_time)){
            $pay_time = explode('~', $pay_time);
            $where[] = ['ol.pay_time','>=',strtotime($pay_time[0])];
            $where[] = ['ol.pay_time','<',strtotime($pay_time[1])];
        }
        if(!empty($start_time)){
            $time_data = searchTime($start_time);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['o.begin_time','>=',strtotime($begin_time)];
            $where[] = ['o.begin_time','<=',strtotime($end_time)];
        }
        $where[] = ['o.pay_type','!=',Order::ORDER_PAY_TYPE_HUABEI];
        $where[] = ['ol.status',OrderLease::ORDER_LEASE_STATUS_PAID];
        $where1[] = ['ol.settle_type',$settle_status];
        $whereSearch = array_merge($where,$where1);
        $order = OrderLease::from('sp_order_lease_2 as ol')->where($whereSearch)
            ->wherein('o.status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))
            ->leftjoin('sp_order_2 as o','o.id','ol.order_id')
            ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
            ->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')
            ->leftjoin('sp_member_2 as m','o.uid','m.id')
            ->select('ol.id as lease_id','ol.order_id','o.trade_no','o.rent_total','o.seller_id','o.month_total',
                'ol.which_period','o.begin_time','o.end_time','og.goods_title','og.lease_set'
                ,'og.lease_time','ol.begin_time as lease_time','ol.pay_type'
                ,'ol.pay_time','ol.rent','ol.paid_rent','ol.status as lease_status',
                    'ol.settle_amount','ol.settle_time','m.cert_name as realname','o.mobile',
                'o.prov','o.city','o.area','o.address','o.financing_type',
                'o.status as order_status','ol.overdue_days','ol.settle_type',
                'ol.reason','s.shopname','o.sync_status','og.is_buy_out','og.pact_type','og.setmeal_foregift','o.amount','o.is_insurance_choose','o.is_insurance_choose_custom')->orderBy('ol.id','desc')->paginate($pageSize);

        $order = obToArray($order);
        if(!empty($order['data'])){
            foreach ($order['data'] as $k=>$v){
                $order['data'][$k]['order_type'] = OrderService::getOrderTypeName($v['order_status']);
                $order['data'][$k]['order_time'] = date('Y-m-d',strtotime($v['begin_time'])).'至'.date('Y-m-d',strtotime($v['end_time']));
                if($v['lease_set'] = GoodsService::GOODS_RENT_TYPE_MONTH){//按月
                    $order['data'][$k]['lease_time'] = '共'.$v['lease_time'].'月';
                }else{
                    $order['data'][$k]['lease_time'] = '共'.$v['lease_time'].'天';
                }
                $order['data'][$k]['pay_type'] = OrderLease::ORDER_LEASE_PAY_TYPE_LIST[$v['pay_type']];
                $order['data'][$k]['lease_time'] = date('Y-m-d',$v['lease_time']);
                $order['data'][$k]['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
                $order['data'][$k]['lease_status'] = OrderLease::ORDER_LEASE_STATUS_LIST[$v['lease_status']];
                if($v['settle_type'] == OrderLease::ORDER_LEASE_SETTLE_STATUS_YES){//已结算
                    $trans = ShopTrans::getShopTransByOrder($v['order_id'],$v['lease_id']);
                    if(!empty($trans)){
                        $order['data'][$k]['service_fee'] = $trans['order_fixed_cost'].'+'.$trans['order_rate'].'%('.$trans['service_fee'].'元)';
                    }else{
                        $order['data'][$k]['service_fee'] = '';
                    }
                }else{
                    $order['data'][$k]['service_fee'] = '';
                }
                $order['data'][$k]['lease_is_settle'] = 1;
                $order['data'][$k]['financing_type'] = Order::ORDER_FINANCING_TYPE_NAME_LIST[$v['financing_type']];
                $order['data'][$k]['order_status'] = OrderService::ORDER_STATUS_NAME_LIST[$v['order_status']];
                $order['data'][$k]['settle_type_name'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_LIST[$v['settle_type']];
                if($v['pact_type']==1){
                    $order['data'][$k]['order_total_price'] =bcadd($v['setmeal_foregift']*$v['amount'],GoodsService::getGoodsInsurance($v['is_insurance_choose'],$v['is_insurance_choose_custom']),2);
                }else{
                    $order['data'][$k]['order_total_price'] =$v['rent_total'];
                }
            }
        }

        $order_count = OrderLease::from('sp_order_lease_2 as ol')->where($where)->wherein('o.status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))->leftjoin('sp_order_2 as o','o.id','ol.order_id')->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')->select('ol.settle_type',DB::raw('count(*) as count'))->groupBy(['ol.settle_type'])->get();
        $order_count = obToArray($order_count);
        $count['no_settle'] = 0;
        $count['settled'] = 0;
        $count['refuse_settle'] = 0;
        if(!empty($order_count)){
            foreach ($order_count as $item){
                if($item['settle_type'] == OrderLease::ORDER_LEASE_SETTLE_STATUS_NO){
                    $count['no_settle'] = $item['count'];
                }
                if($item['settle_type'] == OrderLease::ORDER_LEASE_SETTLE_STATUS_YES){
                    $count['settled'] = $item['count'];
                }
                if($item['settle_type'] == OrderLease::ORDER_LEASE_SETTLE_STATUS_REFUSE){
                    $count['refuse_settle'] = $item['count'];
                }
            }
        }
        $order['count'] = $count;

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$order);
    }

    /**
     * 账单结算列表导出
     */
    public function orderLeaseSettleExport(Request $request){
        $settle_status = $request->input('settle_status',OrderLease::ORDER_LEASE_SETTLE_STATUS_NO);
        $order_no = $request->input('order_no','');
        $user_name = $request->input('user_name','');
        $shop_name = $request->input('shop_name','');
        $lease_term = $request->input('lease_term','');
        $pay_time = $request->input('pay_time','');
        $start_time = $request->input('start_time','');
        $where = [];
        if(!empty($order_no)){
            $where[] = ['o.trade_no','like','%'.$order_no.'%'];
        }
        if(!empty($user_name)){
            $where[] = ['m.cert_name','like','%'.$user_name.'%'];
        }
        if(!empty($shop_name)){
            $where[] = ['s.shopname','like','%'.$shop_name.'%'];
        }
        if(!empty($lease_term)){
            $where[] = ['ol.which_period',$lease_term];
        }
        if(!empty($pay_time)){
            $pay_time = explode('~', $pay_time);
            $where[] = ['ol.pay_time','>=',strtotime($pay_time[0])];
            $where[] = ['ol.pay_time','<',strtotime($pay_time[1])];
        }
        if(!empty($start_time)){
            $time_data = searchTime($start_time);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['o.begin_time','>=',strtotime($begin_time)];
            $where[] = ['o.begin_time','<=',strtotime($end_time)];
        }
        $where[] = ['o.pay_type','!=',Order::ORDER_PAY_TYPE_HUABEI];
        $where[] = ['ol.status',OrderLease::ORDER_LEASE_STATUS_PAID];
        $where1[] = ['ol.settle_type',$settle_status];
        $whereSearch = array_merge($where,$where1);
        $order = OrderLease::from('sp_order_lease_2 as ol')->where($whereSearch)
            ->wherein('o.status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))
            ->leftjoin('sp_order_2 as o','o.id','ol.order_id')
            ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
            ->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')
            ->leftjoin('sp_member_2 as m','o.uid','m.id')
            ->select('ol.id as lease_id','ol.order_id','o.trade_no','o.month_total','ol.which_period','o.rent_total',
                'o.begin_time','o.end_time','og.lease_set','og.lease_time','ol.begin_time as lease_time',
                'ol.pay_type','ol.pay_time','ol.rent','ol.paid_rent','ol.status as lease_status',
                'ol.settle_amount','ol.settle_time','m.cert_name as realname','o.mobile','o.prov','o.city','o.area',
                'o.address','o.financing_type','o.status as order_status','ol.overdue_days','ol.settle_type',
                'ol.reason','s.shopname','o.sync_status','og.is_buy_out','og.pact_type','og.setmeal_foregift',
                'o.amount','o.is_insurance_choose','o.is_insurance_choose_custom')->orderBy('ol.id','desc')->get();
        $order = obToArray($order);
        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $data = [];
        if(!empty($order)){
            foreach ($order as $k=>$v){
                $v['order_type'] = OrderService::getOrderTypeName($v['order_status']);
                $v['order_time'] = date('Y-m-d',strtotime($v['begin_time'])).'至'.date('Y-m-d',strtotime($v['end_time']));
                $v['pay_type'] = OrderLease::ORDER_LEASE_PAY_TYPE_LIST[$v['pay_type']];
                $v['lease_time'] = date('Y-m-d',$v['lease_time']);
                $v['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
                $v['lease_status'] = OrderLease::ORDER_LEASE_STATUS_LIST[$v['lease_status']];
                if($v['settle_type'] == OrderLease::ORDER_LEASE_SETTLE_STATUS_YES){//已结算
                    $trans = ShopTrans::getShopTransByOrder($v['order_id'],$v['lease_id']);
                    $v['service_fee'] = $trans['order_fixed_cost'].'+'.$trans['order_rate'].'%';
                }else{
                    $v['service_fee'] = '';
                }
                $v['financing_type'] = Order::ORDER_FINANCING_TYPE_NAME_LIST[$v['financing_type']];
                $v['order_status'] = OrderService::ORDER_STATUS_NAME_LIST[$v['order_status']];
                $v['settle_type_name'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_LIST[$v['settle_type']];

                if($v['pact_type']==1){
                    $order_total_price =bcadd($v['setmeal_foregift']*$v['amount'],GoodsService::getGoodsInsurance($v['is_insurance_choose'],$v['is_insurance_choose_custom']),2);
                }else{
                    $order_total_price =$v['rent_total'];
                }
                $shopName = $v['shopname'];
                $data[] = [
                    'id'=>' '.$v['lease_id'],
                    'trade_no'=>' '.$v['trade_no'],
                    'pact_type'=>' '.$v['pact_type']==1?'固定租期':'租完即送',
                    'order_total_price'=>$order_total_price,
                    'month_total'=>$v['which_period'].'/'.$v['month_total'],
                    'order_time'=>$v['order_time'],
                    'order_type'=>$v['order_type'],
                    'lease_time'=>$v['lease_time'],
                    'pay_time'=>$v['pay_time'],
                    'rent'=>$v['rent'],
                    'paid_rent'=>$v['paid_rent'],
                    'lease_status'=>$v['lease_status'],
                    'overdue_days'=>$v['overdue_days'],
                    'settle_amount'=>$v['settle_amount'],
                    'order_fixed_cost'=>$v['service_fee'],
                    'service_fee'=>$trans['service_fee'],
                    'settle_time'=>$v['settle_time'],
                    'reason'=>$v['reason'],
                    'realname'=>$v['realname'],
                    'mobile'=>' '.$v['mobile'],
                    'address'=>$v['prov'].$v['city'].$v['area'].$v['address'],
                    'financing_type'=>$v['financing_type'],
                    'order_status'=>$v['order_status'],
                    'settle_type_name'=>$v['settle_type_name'],
                    'shopname'=>$shopName,
                ];
            }
        }
        $params = $request->input();
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_SETTLE_LIST);
    }

    /**
     * 获取结算详情
     */
    public function getSettleData(Request $request){
        $lease_id = $request->input('lease_id','');
        if(!empty($lease_id)){
//            $order = OrderLease::from('sp_order_lease_2 as ol')->where('ol.id',$lease_id)
//                ->wherein('ol.status',[OrderLease::ORDER_LEASE_STATUS_PAID,OrderLease::ORDER_LEASE_STATUS_BUYOUT])
//                ->leftjoin('sp_order_2 as o','o.id','ol.order_id')
//                ->leftjoin('sp_order_rate as oe','o.id','oe.order_id')
//                ->select('o.trade_no','o.month_total','ol.which_period','ol.which_period','oe.is_preposition','ol.rent',
//                    'o.rent_total','oe.fixed_amount','oe.shop_rate','o.financing_type',
//                    'o.is_skip','oe.smartcontract_amount','oe.realname_amount','oe.risk_amount',
//                    'oe.platform_rate','oe.withhold_rate','oe.file_rate','oe.financing_rate','oe.insure_rate')->first();
            $order = OrderLease::from('sp_order_lease_2 as ol')->where('ol.id',$lease_id)
                ->wherein('ol.status',[OrderLease::ORDER_LEASE_STATUS_PAID,OrderLease::ORDER_LEASE_STATUS_BUYOUT])
                ->leftjoin('sp_order_2 as o','o.id','ol.order_id')
                ->leftjoin('sp_order_goods_2 as og','og.order_id','o.id')
                ->select('o.trade_no','o.month_total','o.order_from','og.is_buy_out','og.pact_type','o.seller_id','ol.which_period','ol.which_period','ol.rent',
                    'o.rent_total','o.financing_type','o.is_insurance_choose','o.is_insurance_choose_custom',
                    'o.is_skip','o.is_skip','og.goods_id','o.amount', 'og.setmeal_foregift')->first();
            $order = obToArray($order);
            if(!empty($order)){
                if($order['is_skip'] == Order::RISK_SKIP_YES){
                    $order['is_risk'] = '否';
                }else{
                    $order['is_risk'] = '是';
                }
                $settle_data = ShopTrans::getSettleData($order);

                if(empty($settle_data)){
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单来源费率未设置');
                }
                if(!empty($settle_data)){
                    $order=array_merge($order,$settle_data['trans_detail']);
                    if($order['pact_type']==1){
                        $order['rent_total']=bcadd($order['setmeal_foregift']*$order['amount'],GoodsService::getGoodsInsurance($order['is_insurance_choose'],$order['is_insurance_choose_custom']),2);
                    }else{
                        $order['rent_total'] =$order['rent_total'];
                    }

                    if($settle_data['is_preposition']==1&&$order['which_period']==1) {
                        $order['trade_no']=$order['trade_no'].'(前置结算)'.($settle_data['post_deduction']>0?'另后置结算'.$settle_data['post_deduction'].'%':'');
                        $order['risk_amount'] = $settle_data['trans_detail']['risk_amount'];
                        $order['smartcontract_amount'] = $settle_data['trans_detail']['contract_amount'];
                        $order['realname_amount'] = $settle_data['trans_detail']['realname_amount'];
                        $order['fixed_amount'] = $settle_data['fixed_fee'];
                        $order['shop_rate'] = $settle_data['fee_rate'];
                        $order['service_fee'] = sprintf("%.4f",$order['rent_total'] * $settle_data['fee_rate'] * 0.01 + $settle_data['fixed_fee']+bcmul($order['rent'] , $settle_data['post_deduction'] * 0.01,2));
                        $order['settle_amount'] = sprintf("%.4f",$order['rent'] - $order['service_fee']);
                    }else if($settle_data['is_preposition']==1){
                        $order['trade_no']=$order['trade_no'].'(已前置结算)'.($settle_data['post_deduction']>0?'另后置结算'.$settle_data['post_deduction'].'%':'');
                        $order['risk_amount'] = 0;
                        $order['smartcontract_amount'] = 0;
                        $order['realname_amount'] = 0;
                        $order['fixed_amount'] =0;
                        $order['shop_rate'] = 0;
                        $order['service_fee'] = bcmul($order['rent'] , $settle_data['post_deduction'] * 0.01,2);
                        $order['settle_amount'] = sprintf("%.4f",$order['rent'] - $order['service_fee']);
                    }else{
                        $order['risk_amount'] = $settle_data['trans_detail']['risk_amount'];
                        $order['smartcontract_amount'] = $settle_data['trans_detail']['contract_amount'];
                        $order['realname_amount'] = $settle_data['trans_detail']['realname_amount'];
                        $order['fixed_amount'] = $settle_data['fixed_fee'];
                        $order['shop_rate'] = $settle_data['fee_rate'];
                        $order['service_fee'] = sprintf("%.4f",$order['rent'] * $settle_data['fee_rate'] * 0.01 + $settle_data['fixed_fee']);
                        $order['settle_amount'] = sprintf("%.4f",$order['rent'] - $order['service_fee']);
                    }
                }else{
                    $order['service_fee'] = 0;
                    $order['settle_amount'] = 0;
                }
                $order['financing_type'] = Order::ORDER_FINANCING_TYPE_NAME_LIST[$order['financing_type']];

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$order);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要查看的账单');
        }
    }

    /**
     * 账单结算--单期结算
     */
    public function orderSettle(Request $request){
        $lease_id = $request->input('lease_id');
        if(!empty($lease_id)){
            $order = OrderLease::from('sp_order_lease_2 as ol')->where('ol.id',$lease_id)
                ->wherein('ol.status',[OrderLease::ORDER_LEASE_STATUS_PAID,OrderLease::ORDER_LEASE_STATUS_BUYOUT])
                ->leftjoin('sp_order_2 as o','o.id','ol.order_id')
                ->leftjoin('sp_order_goods_2 as og','og.order_id','o.id')
                ->select('o.id as order_id','o.trade_no','o.month_total','og.is_buy_out','og.pact_type','o.order_from','o.seller_id','ol.which_period','ol.which_period','ol.rent',
                    'o.uid','ol.alipay_trade_no','o.is_insurance_choose','o.is_insurance_choose_custom',
                    'o.rent_total','o.financing_type',
                    'o.is_skip','o.is_skip','og.goods_id','o.amount', 'og.setmeal_foregift')->first();


            $order = obToArray($order);
//            dd($order['is_preposition']);
            if(!empty($order)){
                $settle_data = ShopTrans::getSettleData($order);
                if(empty($settle_data)){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单来源费率未设置');
                }
                if(!empty($settle_data)){
                    unset($settle_data['trans_detail']['insure_rate']);

                    $order=array_merge($order,$settle_data['trans_detail']);
                    if($order['pact_type']==1){
                        $order['rent_total']=bcadd($order['setmeal_foregift']*$order['amount'],GoodsService::getGoodsInsurance($order['is_insurance_choose'],$order['is_insurance_choose_custom']),2);
                    }else{
                        $order['rent_total'] =$order['rent_total'];
                    }


                    if($settle_data['is_preposition']==1&&$order['which_period']==1) {
                        $service_fee = sprintf("%.4f", $order['rent_total'] * $settle_data['fee_rate'] * 0.01 + $settle_data['fixed_fee']+bcmul($order['rent'] , $settle_data['post_deduction'] * 0.01,2));
                        $settle_amount = sprintf("%.4f", $order['rent'] - $service_fee);
                        $remarks = '前置结算，第' . $order['which_period'] . '期结算';
                        $settle_data['trans_detail']['platform_amount'] = sprintf("%.4f", $order['rent_total'] * $settle_data['trans_detail']['platform_rate'] * 0.01);
                        $settle_data['trans_detail']['insurance_amount'] = sprintf("%.4f", $order['rent_total'] * $settle_data['trans_detail']['insurance_rate'] * 0.01);
                        $settle_data['trans_detail']['insurance_amount'] = sprintf("%.4f", $order['rent_total'] * $settle_data['trans_detail']['financing_rate'] * 0.01);
                        $settle_data['trans_detail']['file_amount'] = sprintf("%.4f", $order['rent_total'] * $settle_data['trans_detail']['file_rate'] * 0.01);
                        $settle_data['trans_detail']['withhold_amount'] = sprintf("%.4f", $order['rent_total'] * $settle_data['trans_detail']['withhold_rate'] * 0.01);

                        $res = ShopTrans::addOrderTrans($order, $settle_amount, $service_fee, $order['alipay_trade_no'],
                            $remarks, $lease_id, $settle_data['fee_rate'], $settle_data['fixed_fee'],
                            $settle_data['trans_detail'], ShopTrans::ORDER_TRANS_TYPE_SETTLE);
                        if ($res) {
                            $data['settle_type'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_YES;
                            $data['settle_amount'] = $settle_amount;
                            $data['settle_time'] = date('Y-m-d H:i:s');
                            $ol_res = OrderLease::where('id', $lease_id)->update($data);
                            if($ol_res){
                                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'结算成功');
                            }else{
                                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败');
                            }
                        }else{
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败');
                        }
                    }else if($settle_data['is_preposition']==1){
                        $service_fee = bcmul($order['rent'] , $settle_data['post_deduction'] * 0.01,2);
                        $settle_amount = sprintf("%.4f",$order['rent'] - $service_fee);
                        $remarks = '已前置结算，第' . $order['which_period'] . '期结算';
                        $settle_data['trans_detail']['platform_amount'] = 0;
                        $settle_data['trans_detail']['insurance_amount'] = 0;
                        $settle_data['trans_detail']['insurance_amount'] = 0;
                        $settle_data['trans_detail']['file_amount'] =0;
                        $settle_data['trans_detail']['withhold_amount'] = 0;

                        $res = ShopTrans::addOrderTrans($order,$settle_amount,$service_fee,$order['alipay_trade_no'],$remarks,$lease_id,$settle_data['fee_rate'],$settle_data['fixed_fee'],$settle_data['trans_detail'],ShopTrans::ORDER_TRANS_TYPE_SETTLE);
                        if($res) {
                            $data['settle_type'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_YES;
                            $data['settle_amount'] = $settle_amount;
                            $data['settle_time'] = date('Y-m-d H:i:s');
                            $ol_res = OrderLease::where('id', $lease_id)->update($data);
                            if($ol_res){
                                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'结算成功');
                            }else{
                                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败1');
                            }
                        }else{
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败2');
                        }
                    }else{
                        $service_fee = sprintf("%.4f",$order['rent'] * $settle_data['fee_rate'] * 0.01 + $settle_data['fixed_fee']);
                        $settle_amount = sprintf("%.4f",$order['rent'] - $service_fee);
                        $remarks = '第' . $order['which_period'] . '期结算';
                        $settle_data['trans_detail']['platform_amount'] = sprintf("%.4f",$order['rent'] * $settle_data['trans_detail']['platform_rate'] * 0.01);
                        $settle_data['trans_detail']['insurance_amount'] = sprintf("%.4f",$order['rent'] * $settle_data['trans_detail']['insurance_rate']* 0.01);
                        $settle_data['trans_detail']['insurance_amount'] = sprintf("%.4f",$order['rent'] * $settle_data['trans_detail']['financing_rate']* 0.01);
                        $settle_data['trans_detail']['file_amount'] = sprintf("%.4f",$order['rent'] * $settle_data['trans_detail']['file_rate']* 0.01);
                        $settle_data['trans_detail']['withhold_amount'] = sprintf("%.4f",$order['rent'] * $settle_data['trans_detail']['withhold_rate']* 0.01);

                        $res = ShopTrans::addOrderTrans($order,$settle_amount,$service_fee,$order['alipay_trade_no'],$remarks,$lease_id,$settle_data['fee_rate'],$settle_data['fixed_fee'],$settle_data['trans_detail'],ShopTrans::ORDER_TRANS_TYPE_SETTLE);
                        if($res){
                            $data['settle_type'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_YES;
                            $data['settle_amount'] = $settle_amount;
                            $data['settle_time'] = date('Y-m-d H:i:s');
                            $ol_res = OrderLease::where('id',$lease_id)->update($data);
                            if($ol_res){
                                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'结算成功');
                            }else{
                                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败');
                            }
                        }else{
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败');
                        }
                    }

                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败！');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要结算的账单');
        }
    }


    /**
     * 拒绝结算
     */
    public function orderRefuseSettle(Request $request){
        $lease_id = $request->input('lease_id');
        $refuse_reason = $request->input('refuse_reason');
        if(!empty($lease_id) && !empty($refuse_reason)){
            if(OrderLease::where([['id',$lease_id],['status',OrderLease::ORDER_LEASE_STATUS_PAID]])->count() > 0) {
                $data['reason']      = $refuse_reason;
                $data['settle_type'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_REFUSE;
                $res                 = OrderLease::where('id', $lease_id)->update($data);
                if ($res) {
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '操作成功');
                } else {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '账单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的账单并填写拒绝原因');
        }
    }

    /**
     * 撤销拒绝结算
     */
    public function cancelRefuseSettle(Request $request){
        $lease_id = $request->input('lease_id');
        if(!empty($lease_id)){
            if(OrderLease::where([['id',$lease_id],['status',OrderLease::ORDER_LEASE_STATUS_PAID]])->count() > 0){
                $data['settle_type'] = OrderLease::ORDER_LEASE_SETTLE_STATUS_NO;
                $res = OrderLease::where('id',$lease_id)->update($data);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '账单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的账单');
        }
    }

    /**
     * 花呗订单结算列表
     */
    public function huebeiOrderSettleList(Request $request)
    {
        $settle_status = $request->input('settle_status',Order::ORDER_HUABEI_SETTLE_STATUS_NO);
        $order_no = $request->input('order_no','');
        $user_name = $request->input('user_name','');
        $shop_name = $request->input('shop_name','');
        $lease_term = $request->input('lease_term','');
        $pay_time = $request->input('pay_time','');
        $start_time = $request->input('start_time','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        $where = [];
        if(!empty($order_no)){
            $where[] = ['o.trade_no','like','%'.$order_no.'%'];
        }
        if(!empty($user_name)){
            $where[] = ['o.realname','like','%'.$user_name.'%'];
        }
        if(!empty($shop_name)){
            $where[] = ['s.shopname','like','%'.$shop_name.'%'];
        }
        if(!empty($lease_term)){
            $where[] = ['o.month_total',$lease_term];
        }
        if(!empty($pay_time)){
            $pay_time = explode('~', $pay_time);
            $where[] = ['o.pay_time','>=',strtotime($pay_time[0])];
            $where[] = ['o.pay_time','<',strtotime($pay_time[1])];
        }
        if(!empty($start_time)){
            $time_data = searchTime($start_time);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['o.begin_time','>=',strtotime($begin_time)];
            $where[] = ['o.begin_time','<=',strtotime($end_time)];
        }
        $where0[] = ['o.settlement_status',$settle_status];
        $where[] = ['o.pay_type',OrderLease::LEASE_PAY_TYPE_HUABEI];
        $whereSearch = array_merge($where,$where0);
        $order = Order::from('sp_order_2 as o')->where($whereSearch)->wherein('o.status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))
            ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
            ->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')
            ->select('o.id as order_id','o.trade_no','o.month_total','o.begin_time','o.end_time','o.pay_type','o.pay_time','o.rent_total','o.payment','o.realname','o.mobile','o.prov','o.city','o.area','o.address','o.financing_type','o.status as order_status','o.settlement_status','o.settle_refuse_reason','s.shopname')->orderBy('o.id','desc')->paginate($pageSize);
        $order = obToArray($order);
        if(!empty($order['data'])){
            foreach ($order['data'] as $k=>$v){
                $order['data'][$k]['order_type'] = OrderService::getOrderTypeName($v['order_status']);
                $order['data'][$k]['order_time'] = date('Y-m-d',strtotime($v['begin_time'])).'至'.date('Y-m-d',strtotime($v['end_time']));
                $order['data'][$k]['pay_type'] = '花呗分期';
                $order['data'][$k]['pay_status'] = '已支付';
                $order['data'][$k]['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
                if($v['settlement_status'] == Order::ORDER_HUABEI_SETTLE_STATUS_YES){//已结算
                    $trans = ShopTrans::getHuabeiOrderShopTrans($v['order_id']);
                    $order['data'][$k]['settle_amount'] = $trans['amount'];
                    $order['data'][$k]['settle_time'] = $trans['create_time'];
                    $order['data'][$k]['service_fee'] = $trans['order_fixed_cost'].'+'.$trans['order_rate'].'%('.$trans['service_fee'].'元)';
                }else{
                    $order['data'][$k]['service_fee'] = '';
                    $order['data'][$k]['settle_amount'] = '';
                    $order['data'][$k]['settle_time'] = '';
                }
                $order['data'][$k]['huabei_fee_bear'] = GoodsService::GOODS_HUABEI_FEE_LIST[$v['huabei_fee_bear']];
                $order['data'][$k]['financing_type'] = Order::ORDER_FINANCING_TYPE_NAME_LIST[$v['financing_type']];
                $order['data'][$k]['order_status'] = OrderService::ORDER_STATUS_NAME_LIST[$v['order_status']];
                $order['data'][$k]['settlement_status_name'] = Order::ORDER_HUABEI_SETTLE_STATUS_LIST[$v['settlement_status']];
            }
        }

        $order_count = Order::from('sp_order_2 as o')->where($where)->wherein('o.status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')->select('o.settlement_status',DB::raw('count(*) as count'))->groupBy(['o.settlement_status'])->get();
        $order_count = obToArray($order_count);
        $count['no_settle'] = 0;
        $count['settled'] = 0;
        $count['refuse_settle'] = 0;
        if(!empty($order_count)){
            foreach ($order_count as $item){
                if($item['settlement_status'] == Order::ORDER_HUABEI_SETTLE_STATUS_NO){
                    $count['no_settle'] = $item['count'];
                }
                if($item['settlement_status'] == Order::ORDER_HUABEI_SETTLE_STATUS_YES){
                    $count['settled'] = $item['count'];
                }
                if($item['settlement_status'] == Order::ORDER_HUABEI_SETTLE_STATUS_REFUSE){
                    $count['refuse_settle'] = $item['count'];
                }
            }
        }
        $order['count'] = $count;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$order);

    }

    /**
     * 花呗结算列表导出
     */
    public function huebeiOrderSettleExport(Request $request){
        $settle_status = $request->input('settle_status',Order::ORDER_HUABEI_SETTLE_STATUS_NO);
        $order_no = $request->input('order_no','');
        $user_name = $request->input('user_name','');
        $shop_name = $request->input('shop_name','');
        $lease_term = $request->input('lease_term','');
        $pay_time = $request->input('pay_time','');
        $start_time = $request->input('start_time','');
        $where = [];
        if(!empty($order_no)){
            $where[] = ['o.trade_no','like','%'.$order_no.'%'];
        }
        if(!empty($user_name)){
            $where[] = ['o.realname','like','%'.$user_name.'%'];
        }
        if(!empty($shop_name)){
            $where[] = ['s.shopname','like','%'.$shop_name.'%'];
        }
        if(!empty($lease_term)){
            $where[] = ['o.month_total',$lease_term];
        }
        if(!empty($pay_time)){
            $pay_time = explode('~', $pay_time);
            $where[] = ['o.pay_time','>=',strtotime($pay_time[0])];
            $where[] = ['o.pay_time','<',strtotime($pay_time[1])];
        }
        if(!empty($start_time)){
            $time_data = searchTime($start_time);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['o.begin_time','>=',strtotime($begin_time)];
            $where[] = ['o.begin_time','<=',strtotime($end_time)];
        }
        $where0[] = ['o.settlement_status',$settle_status];
        $where[] = ['o.pay_type',OrderLease::LEASE_PAY_TYPE_HUABEI];
        $whereSearch = array_merge($where,$where0);
        $order = Order::from('sp_order_2 as o')->where($whereSearch)->wherein('o.status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))
            ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
            ->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')
            ->select('o.id as order_id','o.trade_no','o.month_total','o.begin_time','o.end_time','o.pay_type','o.pay_time','o.rent_total','o.payment','o.realname','o.mobile','o.prov','o.city','o.area','o.address','o.financing_type','o.status as order_status','o.settlement_status','o.settle_refuse_reason','s.shopname')->orderBy('o.id','desc')->get();
        $order = obToArray($order);
        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $data = [];
        if(!empty($order['data'])){
            foreach ($order['data'] as $k=>$v){
                $v['order_type'] = OrderService::getOrderTypeName($v['order_status']);
                $v['order_time'] = date('Y-m-d',strtotime($v['begin_time'])).'至'.date('Y-m-d',strtotime($v['end_time']));
                $v['pay_type'] = '花呗分期';
                $v['pay_status'] = '已支付';
                $v['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
                if($v['settlement_status'] == Order::ORDER_HUABEI_SETTLE_STATUS_YES){//已结算
                    $trans = ShopTrans::getHuabeiOrderShopTrans($v['order_id']);
                    $v['settle_amount'] = $trans['amount'];
                    $v['settle_time'] = $trans['create_time'];
                    $v['service_fee'] = $trans['order_fixed_cost'].'+'.$trans['order_rate'].'%('.$trans['service_fee'].'元)';
                }else{
                    $v['service_fee'] = '';
                    $v['settle_amount'] = '';
                    $v['settle_time'] = '';
                }
                $v['huabei_fee_bear'] = GoodsService::GOODS_HUABEI_FEE_LIST[$v['huabei_fee_bear']];
                $v['financing_type'] = Order::ORDER_FINANCING_TYPE_NAME_LIST[$v['financing_type']];
                $v['order_status'] = OrderService::ORDER_STATUS_NAME_LIST[$v['order_status']];
                $v['settlement_status_name'] = Order::ORDER_HUABEI_SETTLE_STATUS_LIST[$v['settlement_status']];
                $data[] = [
                    'id'=>' '.$v['order_id'],
                    'trade_no'=>' '.$v['trade_no'],
                    'month_total'=>$v['month_total'],
                    'order_time'=>$v['order_time'],
                    'order_type'=>$v['pay_type'],
                    'pay_time'=>$v['pay_time'],
                    'rent_total'=>$v['rent_total'],
                    'paid_rent'=>$v['payment'],
                    'huabei_fee_bear'=>$v['huabei_fee_bear'],
                    'settle_amount'=>$v['settle_amount'],
                    'service_fee'=>$v['service_fee'],
                    'settle_time'=>$v['settle_time'],
                    'pay_status'=>$v['pay_status'],
                    'realname'=>$v['realname'],
                    'mobile'=>' '.$v['mobile'],
                    'address'=>$v['prov'].$v['city'].$v['area'].$v['address'],
                    'financing_type'=>$v['financing_type'],
                    'order_status'=>$v['order_status'],
                    'settle_type'=>$v['settlement_status_name'],
                    'shopname'=>$v['shopname'],
                ];
            }
        }
        $params = $request->input();
        ExportService::exportFile($params,$data,ExportService::FILE_HUABEI_ORDER_SETTLE_LIST);
    }

    /**
     * 获取花呗结算详情
     */
    public function getHuabeiOrderSettleData(Request $request){
        $order_id = $request->input('order_id','');
        if(!empty($order_id)){
            $order = Order::from('sp_order_2 as o')->where([['o.id',$order_id],['o.pay_type',OrderLease::LEASE_PAY_TYPE_HUABEI]])
                ->leftjoin('sp_order_rate as oe','o.id','oe.order_id')
                ->select('o.trade_no','o.month_total','o.huabei_stages','o.rent_total','oe.fixed_amount','oe.shop_rate','o.financing_type','o.is_skip','oe.smartcontract_amount','oe.realname_amount','oe.risk_amount','oe.platform_rate','oe.withhold_rate','oe.file_rate','oe.financing_rate','oe.insure_rate','oe.huabei_rate')->first();
            $order = obToArray($order);
            if(!empty($order)){
                $order['huabei_fee'] = 0;
                if($order['huabei_fee_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT){
                    $order['huabei_fee'] = sprintf("%.4f",$order['rent_total']*$order['huabei_rate']/100);
                }
                $settle_data = ShopTrans::getHuabeiSettleData($order);
                if(!empty($settle_data)){
                    $order['shop_rate'] = $settle_data['fee_rate'];
                    $order['risk_amount'] = $settle_data['trans_detail']['risk_amount'];
                    $order['service_fee'] = sprintf("%.4f",$order['rent_total'] * $settle_data['fee_rate'] * 0.01 + $settle_data['fixed_fee']);
                    $order['settle_amount'] = sprintf("%.4f",$order['rent_total'] - $order['service_fee']);
                }else{
                    $order['service_fee'] = 0;
                    $order['settle_amount'] = 0;
                }
                $order['platform_rate'] = sprintf("%.4f",$settle_data['fee_rate'] - $order['huabei_rate']);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$order);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要查看的账单');
        }
    }


    /**
     * 花呗订单结算
     */
    public function huabeiOrderSettle(Request $request){
        $order_id = $request->input('order_id');
        if(!empty($order_id)){
            $order = Order::from('sp_order_2 as o')
                ->leftjoin('sp_order_rate as oe','o.id','oe.order_id')
                ->where([['o.id',$order_id],['o.pay_type',OrderLease::LEASE_PAY_TYPE_HUABEI]])
                ->wherein('status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))
                ->select('o.id as order_id','o.uid','o.seller_id','o.trade_no','o.month_total','o.month_total','o.rent_total','o.alipay_trade_no','oe.fixed_amount','oe.shop_rate','o.financing_type','o.is_skip','oe.smartcontract_amount','oe.realname_amount','oe.risk_amount','oe.platform_rate','oe.withhold_rate','oe.file_rate','oe.financing_rate','oe.insure_rate','oe.huabei_rate')->first();
            $order = obToArray($order);
            if(!empty($order)){
                if(OrderRate::where('order_id',$order['order_id'])->count() == 0){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单来源费率未设置');
                }
                $settle_data = ShopTrans::getHuabeiSettleData($order);
                if(!empty($settle_data)){
                    $huabei_fee = sprintf("%.4f",$order['rent_total']*$order['huabei_rate'] * 0.01);
                    $service_fee = sprintf("%.4f",$order['rent_total'] * $settle_data['fee_rate'] * 0.01 + $settle_data['fixed_fee']);
                    $settle_amount = sprintf("%.4f",$order['rent_total'] - $service_fee - $huabei_fee);
                    $settle_data['trans_detail']['huabei_fee'] = $huabei_fee;
                    $remarks = '花呗订单' . $order['trade_no'] . '结算';
                    $settle_data['trans_detail']['platform_amount'] = sprintf("%.4f",$order['rent_total'] * $settle_data['trans_detail']['platform_rate'] * 0.01);
                    $settle_data['trans_detail']['insurance_amount'] = sprintf("%.4f",$order['rent_total'] * $settle_data['trans_detail']['insurance_rate']* 0.01);
                    $settle_data['trans_detail']['insurance_amount'] = sprintf("%.4f",$order['rent_total'] * $settle_data['trans_detail']['financing_rate']* 0.01);
                    $settle_data['trans_detail']['file_amount'] = sprintf("%.4f",$order['rent_total'] * $settle_data['trans_detail']['file_rate']* 0.01);
                    $settle_data['trans_detail']['withhold_amount'] = sprintf("%.4f",$order['rent_total'] * $settle_data['trans_detail']['withhold_rate']* 0.01);
                    $res = ShopTrans::addOrderTrans($order,$settle_amount,$service_fee,$order['alipay_trade_no'],$remarks,'',$settle_data['fee_rate'],$settle_data['fixed_fee'],$settle_data['trans_detail'],ShopTrans::ORDER_TRANS_TYPE_SETTLE);
                    if($res){
                        $o_res = Order::where('id',$order_id)->update(['settlement_status'=>Order::ORDER_HUABEI_SETTLE_STATUS_YES]);
                        $lease_res = OrderLease::where('order_id',$order_id)->update(['settle_type' => OrderLease::ORDER_LEASE_SETTLE_STATUS_YES,'settle_time' => date('Y-m-d H:i:s')]);
                        if($o_res && $lease_res){
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'结算成功');
                        }else{
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败');
                        }
                    }else{
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败');
                    }
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '结算失败！');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要结算的账单');
        }
    }



    /**
     * 花呗订单拒绝结算
     */
    public function huabeiOrderRefuseSettle(Request $request){
        $order_id = $request->input('order_id');
        $refuse_reason = $request->input('refuse_reason');
        if(!empty($order_id) && !empty($refuse_reason)){
            if(Order::where([['id',$order_id],['pay_type',OrderLease::LEASE_PAY_TYPE_HUABEI]])->wherein('status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))->count() > 0){
                $data['settle_refuse_reason'] = $refuse_reason;
                $data['settlement_status'] = Order::ORDER_HUABEI_SETTLE_STATUS_REFUSE;
                $res = Order::where('id',$order_id)->update($data);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的账单并填写拒绝原因');
        }
    }

    /**
     * 花呗订单撤销拒绝结算
     */
    public function huabeiCancelRefuseSettle(Request $request){
        $order_id = $request->input('order_id');
        if(!empty($order_id)){
            if(Order::where([['id',$order_id],['pay_type',OrderLease::LEASE_PAY_TYPE_HUABEI]])->wherein('status',OrderService::getOrderStatus(OrderService::ORDER_SETTLE_STATUS))->count() > 0){
                $data['settlement_status'] = Order::ORDER_HUABEI_SETTLE_STATUS_NO;
                $res = Order::where('id',$order_id)->update($data);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的账单');
        }
    }


    /**
     * 订单退款审核列表
     */
    public function orderRefundCheckList(Request $request){
        $refund_status = $request->input('refund_status',OrderRefund::ORDER_REFUND_SHOP_CHECKED);
        $order_no = $request->input('order_no','');
        $shop_name = $request->input('shop_name','');
        $pay_type = $request->input('pay_type','');
        $create_time = $request->input('create_time','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        $where = [];
        if(!empty($order_no)){
            $where[] = ['o.trade_no','like','%'.$order_no.'%'];
        }
        if(!empty($shop_name)){
            $where[] = ['s.shopname','like','%'.$shop_name.'%'];
        }
        if(!empty($pay_type)){
            $where[] = ['o.pay_type',$pay_type];
        }
        if(!empty($create_time)){
            $time_data = searchTime($create_time);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['or.create_time','>=',strtotime($begin_time)];
            $where[] = ['or.create_time','<=',strtotime($end_time)];
        }
        $where_status[] = ['or.refund_status',$refund_status];
        $where_search = array_merge($where,$where_status);
        $order = OrderRefund::from('sp_order_refund as or')->where($where_search)
            ->leftjoin('sp_order_2 as o','o.id','or.order_id')
            ->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')
            ->select('or.id','o.id as order_id','o.trade_no','o.month_total','o.pay_type','o.pay_time','o.rent_total','or.paid_total_rent','or.refund_amount','or.deduct_amount','o.status as order_status','or.refund_status','or.create_time','s.shopname')->orderBy('or.id','desc')->paginate($pageSize);
        $order = obToArray($order);
        if(!empty($order['data'])){
            foreach ($order['data'] as $k=>$v){
                $order['data'][$k]['order_type'] = OrderService::getOrderTypeName($v['order_status']);
                $order['data'][$k]['pay_type'] = $v['pay_type']?OrderLease::ORDER_LEASE_PAY_TYPE_LIST[$v['pay_type']]:'';
                $order['data'][$k]['order_status'] = $v['order_status']?OrderService::ORDER_STATUS_NAME_LIST[$v['order_status']]:'';
                $order['data'][$k]['refund_status_name'] = $v['refund_status']?OrderRefund::ORDER_REFUND_STATUS_NAME_LIST[$v['refund_status']]:'';
                $order['data'][$k]['settle_amount'] = OrderLease::where([['order_id',$v['order_id']],['settle_type',OrderLease::ORDER_LEASE_SETTLE_STATUS_YES]])->sum('settle_amount');
            }
        }

        $order_count = OrderRefund::from('sp_order_refund as or')->where($where)->leftjoin('sp_order_2 as o','o.id','or.order_id')->leftjoin('sp_shop_user_2 as s','o.seller_id','s.member_id')->select('or.refund_status',DB::raw('count(*) as count'))->groupBy(['or.refund_status'])->get();
        $order_count = obToArray($order_count);
        $count['no_check'] = 0;
        $count['checked'] = 0;
        $count['check_refuse'] = 0;
        if(!empty($order_count)){
            foreach ($order_count as $item){
                if($item['refund_status'] == OrderRefund::ORDER_REFUND_SHOP_CHECKED){
                    $count['no_check'] = $item['count'];
                }
                if($item['refund_status'] == OrderRefund::ORDER_REFUND_SUCCESS){
                    $count['checked'] = $item['count'];
                }
                if($item['refund_status'] == OrderRefund::ORDER_REFUND_ADMIN_REFUSE){
                    $count['check_refuse'] = $item['count'];
                }
            }
        }
        $order['count'] = $count;

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$order);

    }


}
